layui.define(['jquery','table','layer','form','common'], function(exports) {
    var $ = layui.jquery,
        layer = layui.layer,
        form = layui.form,
        common = layui.common,
        table = layui.table;
    var tableRole = null;
    //页面事件列表
    var active = {
        reload: function(){
            //执行重载
            table.reload('table_news', {
                page: {
                    curr: 1
                }
                ,where: {
                    "title": $("#title").val()
                }
            });
        },
        remove: function(ids){
            common.ajaxRemove(prefix + "/remove",ids,function(res){
                active['reload'].call(this);
            });
        }
    };
    var _config = null;
    var view = {
        init:function(config){
            _config = config;
            this.initTable();
            // this.initForm();
        },
        initTable:function(){
            tableRole = table.render({
                elem: '#table_news',
                method: 'POST',
                url: prefix + "/listData", //数据接口
                toolbar: '#table_news_toolbar',
                defaultToolbar:['filter'],
                limit: 20,
                page: true,
                cols: [[{
                    type:'checkbox',
                    sort: false,
                    align:'center',
                    width:40,
                    fixed: 'left'
                },
                    {
                        field: 'title',
                        sort: true,
                        width:150,
                        align:'center',
                        title: '标题'
                    },
                    {
                        field: 'multType',
                        sort: true,
                        width:150,
                        align:'center',
                        title: '图文类型'
                    },
                    {
                        field: 'createTime',
                        title: '创建时间',
                        align: 'center',
                        sort: true
                    },
                    {
                        title: '操作',
                        align: 'center',
                        toolbar:'#col_operation'
                    }]]
            });
            /*监听表格复选框选择
            table.on('checkbox(tableRole)', function (obj) {
                console.log(obj,this.value)
            });
            */
            //表格工具栏按钮事件
            table.on('toolbar(tableNews)', function(obj){
                switch(obj.event){
                    case 'toolbar-news-remove':
                        var checks = table.checkStatus('table_news');
                        if(checks.data.length==0){
                            layer.msg("请至少选择一条数据");
                            break;
                        }
                        var ids = common.joinArray(checks.data,"id");
                        active['remove'].call(this,ids);
                        break;
                    case 'toolbar-multiple-add':
                        var perContent=layer.open({
                            type: 2,
                            title: '添加多图文',
                            shade: false,
                            fixed: false,
                            maxmin: true,
                            area: ['600px', '450px'],
                            content: prefix + '/addmultiple',
                            btn:["创建","取消"],
                            yes:function(index,layero){
                                layui.config({
                                    base: '/js/weixin/msg/news/'
                                }).use('addmultiple',function(){
                                    layui.addmultiple.submit({
                                        "index":index,
                                        "layero":layero
                                    });
                                });
                            }
                        });
                        layer.full(perContent);
                        break;
                    case 'toolbar-single-add':
                        var perContent=layer.open({
                            type: 2,
                            title: '添加单图文',
                            shade: false,
                            fixed: false,
                            maxmin: true,
                            area: ['600px', '450px'],
                            content: prefix + '/addsingle',
                            btn:["创建","取消"],
                            yes:function(index,layero){
                                layui.config({
                                    base: '/js/weixin/msg/news/'
                                }).use('addsingle',function(){
                                    layui.addsingle.submit({
                                        "index":index,
                                        "layero":layero
                                    });
                                });
                            }
                        });
                        layer.full(perContent);
                        break;
                }
            });

            //表格事件
            table.on('tool(tableNews)', function(obj){
                switch(obj.event){
                    case 'del':
                        active['remove'].call(this,obj.data.id);
                        break;
                    case 'edit':
                      var perContent=layer.open({
                            type: 2,
                            title: '编辑',
                            shade: false,
                            fixed: false,
                            maxmin: true,
                            content: prefix + '/edit/'+obj.data.id,
                            btn:["保存","取消"],
                            yes:function(index,layero){
                                layui.config({
                                    base: '/js/weixin/msg/news/'
                                }).use('addsingle',function(){
                                    layui.addsingle.submit({
                                        "index":index,
                                        "layero":layero
                                    });
                                });
                            }
                        });
                        layer.full(perContent);
                        break;
                    case 'mass':
                        layer.open({
                            type: 2,
                            title: '推送',
                            shade: false,
                            fixed: false,
                            maxmin: true,
                            area: ['800px', '600px'],
                            content: prefix + '/edit/'+obj.data.id,
                            btn:["保存","取消"],
                            yes:function(index,layero){
                                layui.config({
                                    base: '/js/role/'
                                }).use('add',function(){
                                    layui.add.submit({
                                        "index":index,
                                        "layero":layero
                                    });
                                });
                            }
                        });
                        break;
                }
            });
        },

        removeItems:function(){
            active['remove'].call(this);
        },
        refreshTable:function(){
            active['reload'].call(this);
        }
    };
    exports('document',view);
});